package com.pcbly.web.components.form;

import java.util.HashMap;
import java.util.List;

import com.pcbly.web.bean.table.ColumnBean;
import com.pcbly.web.bean.table.ColumnPropertyBean;
import com.pcbly.web.components.utils.property.GlobalReadProperties;

@SuppressWarnings("unchecked")
public abstract class AbstractAnalyseCommonDataExt {

	public List<HashMap<String, String>> dataList;
	public List<ColumnBean> columnList;

	public List<HashMap<String, String>> getDataList() {
		return dataList;
	}

	public abstract StringBuilder execute();

	public void setDataList(List<HashMap<String, String>> dataList) {
		this.dataList = dataList;
	}

	public List<ColumnBean> getColumnList() {
		return columnList;
	}

	public void setColumnList(List<ColumnBean> columnList) {
		this.columnList = columnList;
	}

	protected StringBuilder getXml(ColumnBean column, boolean editFlag) {
		StringBuilder sb = new StringBuilder();
		if (column.getColumnStyle().equals("text")) {
			sb.append(text(column, editFlag));
		} else if (column.getColumnStyle().equals("hidden")) {
			sb.append(hidden(column, editFlag));
		} else if (column.getColumnStyle().equals("textarea")) {
			sb.append(textarea(column, editFlag));
		} else if (column.getColumnStyle().equals("file")) {
			sb.append(file(column, editFlag));
		} else if (column.getColumnStyle().equals("checkbox")) {
			sb.append(checkbox(column, editFlag));
		} else if (column.getColumnStyle().equals("radio")) {
			sb.append(radio(column, editFlag));
		} else if (column.getColumnStyle().equals("select")) {
			sb.append(select(column, editFlag));
		}
		return sb;
	}

	protected StringBuilder checkbox(ColumnBean bean, boolean editFlag) {// not
																			// fill
																			// in
		StringBuilder sb = new StringBuilder();
		return sb;
	}

	protected StringBuilder radio(ColumnBean bean, boolean editFlag) {// not
																		// fill
																		// in
		StringBuilder sb = new StringBuilder();
		return sb;
	}

	protected StringBuilder select(ColumnBean bean, boolean editFlag) {// not
																		// fill
																		// in
		StringBuilder sb = new StringBuilder();
		return sb;
	}

	protected StringBuilder textarea(ColumnBean bean, boolean editFlag) {
		StringBuilder sb = new StringBuilder();
		sb.append("<textarea ");
		sb.append(setIdAndName(bean));
		sb.append(setProperty(bean));
		String temp = sb.toString();
		if (!(temp.toLowerCase().indexOf("rows") > 0)) {
			sb.append(" rows=\"5\" ");
		}
		if (!(temp.toLowerCase().indexOf("cols") > 0)) {
			sb.append(" cols=\"50\" ");
		}
		sb.append(setRequire(bean));
		sb.append(setStringTypeAndLength(bean));
		sb.append(">");
		if (editFlag) {
			for (HashMap<String, String> map : dataList) {
				temp = map.get(bean.getColumnName());
				if (!(temp == null || "".equals(temp))) {
					sb.append(temp);
					break;
				}
			}
		}
		sb.append("</textarea>");
		return sb;
	}

	private StringBuilder setProperty(ColumnBean bean) {
		StringBuilder sb = new StringBuilder();
		if (!bean.getColumnPropertyBeans().isEmpty()) {
			List<ColumnPropertyBean> proerties = (List<ColumnPropertyBean>) bean
					.getColumnPropertyBeans();
			if (!proerties.isEmpty() || proerties.size() > 0) {
				for (ColumnPropertyBean property : proerties) {
					sb.append(property.getPropertyName());
					sb.append(" \"");
					sb.append(property.getPropertyValue());
					sb.append("\" ");
				}
			}
		}
		return sb;
	}

	private StringBuilder setIdAndName(ColumnBean bean) {
		StringBuilder sb = new StringBuilder();
		sb.append(" id=\"");
		sb.append(bean.getColumnName());
		sb.append("\" ");
		sb.append(" name=\"");
		sb.append(bean.getColumnName());
		sb.append("\" ");
		sb.append(" extend=\"true");
		sb.append("\" ");

		return sb;
	}

	protected StringBuilder file(ColumnBean bean, boolean editFlag) {
		StringBuilder sb = new StringBuilder();
		sb.append("<input type=\"");
		sb.append("file\" ");
		sb.append(setIdAndName(bean));
		sb.append(setProperty(bean));
		sb.append(">");
		if (editFlag) {
			sb.append("<br><a href=\"");
			sb.append(GlobalReadProperties
					.getStringToProperty("lightsoft.http"));
			for (HashMap<String, String> map : dataList) {
				String temp = map.get(bean.getColumnName());
				if (!(temp == null || "".equals(temp))) {
					sb.append("/");
					sb.append(temp);
					break;
				}
			}
			sb.append("\">");

			sb.append("</a>");
		}
		return sb;
	}

	protected StringBuilder hidden(ColumnBean bean, boolean editFlag) {
		StringBuilder sb = new StringBuilder();
		sb.append("<input type=\"");
		sb.append("hidden\" ");
		sb.append(setIdAndName(bean));
		sb.append(setInput(bean, editFlag));
		sb.append(">");
		return sb;
	}

	private StringBuilder setInput(ColumnBean bean, boolean editFlag) {
		StringBuilder sb = new StringBuilder();
		if (editFlag) {
			sb.append(" value=\"");
			for (HashMap<String, String> map : dataList) {
				String temp = map.get(bean.getColumnName());
				if (!(temp == null || "".equals(temp))) {
					sb.append(temp);
					break;
				}
			}
			sb.append("\" ");
		} else {
			if (!(bean.getDefaultValue() == null || "".equals(bean
					.getDefaultValue().trim()))) {
				sb.append(" value=\"");
				sb.append(bean.getDefaultValue());
				sb.append("\" ");
			}
		}
		return sb;
	}

	private String setRequire(ColumnBean bean) {
		if (bean.getColumnNull() != null && bean.getColumnNull().equals("0")) {
			return " dataRequire=\"true\" ";
		} else {
			return "";
		}
	}

	private String setStringTypeAndLength(ColumnBean bean) {
		StringBuilder sb = new StringBuilder();
		sb.append(" dataType=\"string\" ");
		sb.append(" dataLength=\"");
		if (bean.getColumnType().equals("text")) {
			sb.append(32000);
		} else {
			sb.append(bean.getLength());
		}
		sb.append("\" ");
		return sb.toString();
	}

	protected StringBuilder text(ColumnBean bean, boolean editFlag) {
		StringBuilder sb = new StringBuilder();
		sb.append("<input type=\"");
		sb.append("text\" ");
		sb.append(setIdAndName(bean));
		sb.append(setRequire(bean));
		if (!(bean.getColumnType() == null)) {
			if (bean.getColumnType().indexOf("char") >= 0
					|| bean.getColumnType().indexOf("text") >= 0) {
				sb.append(setStringTypeAndLength(bean));
			} else if (bean.getColumnType().equals("datetime")) {
				sb.append(" readonly=\"true\" ");
			} else {
				sb.append(" dataType=\"");
				sb.append(bean.getColumnType());
				sb.append("\"");
			}
		}

		sb.append(setInput(bean, editFlag));
		sb.append(setProperty(bean));
		sb.append(">");
		if (!(bean.getColumnType() == null)) {
			if (bean.getColumnType().equals("datetime")) {
				sb.append("&nbsp;<img style=\"border: 0px;\" ");
				sb.append(" src=\"");
				sb.append(GlobalReadProperties
						.getStringToProperty("lightsoft.http"));
				sb.append("/images/calendar.gif\" ");
				sb
						.append(" onclick=\"WdatePicker({dateFmt:'yyyy-MM-dd',  el:'");
				sb.append(bean.getColumnName());
				sb.append("'})\">");
			}
		}
		return sb;
	}
}
